import { setUserAddress } from '../../services/user';
import { rawCitiesData } from './cityData'; // 导入城市数据

Page({
  data: {
    userName: '',
    userPhone: '',
    detail: '',
    selectValue: '', // 选择的值
    selectCode: '', // 选择的值
    selectShow: false, // 是否显示级联组件
    regionData: '',
    addressInfo: 0,
    rawCitiesData, // mock的级联数据
    id: 0
  },
  onLoad(option) {
    my.setNavigationBar({
      title: option && option.addressInfo ? '修改地址' : '新增地址',
      backgroundColor: '#4BA3F9'
    })
    if (option.addressInfo) {
      let addressInfo = JSON.parse(option.addressInfo)
      this.setData({
        userName: addressInfo.userName,
        userPhone: addressInfo.userPhone,
        detail: addressInfo.detail,
        id: addressInfo.id,
        addressInfo: addressInfo,
        selectValue: addressInfo.provinceName + ' ' + addressInfo.cityName + ' ' + addressInfo.districtName,
        selectCode: addressInfo.provinceId + ' ' + addressInfo.cityId + ' ' + addressInfo.districtId
      })

    }
  },
  onShow(option) {

  },
  inputname(e) {
    console.log(e)
    this.setData({
      userName: e.detail.value,
    });
  },
  inputphone(e) {
    this.setData({
      userPhone: e.detail.value,
    });
  },
  inputdetail(e) {
    this.setData({
      detail: e.detail.value,
    });
  },
  onSubmitAddress() {
    console.log(1)
    let provinceId = 0
    let cityId = 0
    let districtId = 0
    let provinceName = ''
    let cityName = ''
    let districtName = ''
    if (this.data.selectCode.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '请选择收货地址',
        duration: 2000,
      });
      return
    }
    provinceId = this.data.selectCode.split(' ')[0]
    cityId = this.data.selectCode.split(' ')[1]
    districtId = this.data.selectCode.split(' ')[2]
    provinceName = this.data.selectValue.split(' ')[0]
    cityName = this.data.selectValue.split(' ')[1]
    districtName = this.data.selectValue.split(' ')[2]
    console.log(this.data.userName)
    if (this.data.userName.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '请输入姓名',
        duration: 2000,
      });
      return
    }
    if (this.data.userPhone.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '请输入电话',
        duration: 2000,
      });
      return
    }
    if (this.data.detail.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '请输入您的详细地址',
        duration: 2000,
      });
      return
    }
    let param = {
      provinceId: provinceId,
      cityId: cityId,
      districtId: districtId,
      provinceName: provinceName,
      cityName: cityName,
      districtName: districtName,
      detail: this.data.detail,
      userPhone: this.data.userPhone,
      userName: this.data.userName,
    }
    if (this.data.id > 0) {
      param.userAddressId = this.data.id
    }
    setUserAddress(param).then(res => {
      console.log(res)
      if (res.code == "000000") {
        my.showToast({
          type: 'success',
          content: '保存成功',
          duration: 2000,
        });
        setTimeout(() => {
          my.navigateBack()
        }, 2000);
      }

    })
  },
  onReset() {
  },
  // openMultiLevelSelect() {
  //   my.multiLevelSelect({
  //     title: '多级联选择器',//级联选择标题
  //     list: [
  //       {
  //         name: "杭州市",//条目名称
  //         code: "1001",
  //         subList: [
  //           {
  //             name: "西湖区",
  //             code: "1002",
  //             subList: [
  //               {
  //                 code: "1003",
  //                 name: "古翠街道"
  //               },
  //               {
  //                 code: "1003",
  //                 name: "文新街道"
  //               }
  //             ]
  //           },
  //           {
  //             code: "1004",
  //             name: "上城区",
  //             subList: [
  //               {
  //                 code: "1005",
  //                 name: "延安街道"
  //               },
  //               {
  //                 code: "1005",
  //                 name: "龙翔桥街道"
  //               }
  //             ]
  //           }
  //         ]//级联子数据列表
  //       }],//级联数据列表
  //     success: (res) => {
  //       console.log(res)
  //       this.setData({
  //         regionData: res.result.map((item) => item.name)
  //       })
  //     }
  //   });
  // },
  openSelect() {
    this.setData({ selectShow: true });
  },
  /**
   * 关闭级联组件
   * @method closeSelect
   */
  closeSelect() {
    this.setData({ selectShow: false });
  },
  /**
   * 点击确认触发的事件
   * @method onSelectSuccess
   * @param {*} result
   */
  onSelectSuccess(result) {
    let selectValue = '';
    let selectCode = '';
    result && result.forEach((item) => {
      selectValue += `${item.name} `;
      selectCode += `${item.code} `;
    });
    if (selectValue.length > 0) {
      selectValue = selectValue.substr(0, selectValue.length - 1);
      selectCode = selectCode.substr(0, selectCode.length - 1);
    }
    this.setData({ selectValue, selectCode });

  },

});
